package com.ktjiaoyu.controller;

import com.ktjiaoyu.entity.GoodsCategory;
import com.ktjiaoyu.entity.GoodsInfo;
import com.ktjiaoyu.entity.SysRole;
import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.service.CategoryService;
import com.ktjiaoyu.service.GoodsInfoService;
import com.ktjiaoyu.service.UploadService;
import com.ktjiaoyu.service.impl.SysUserServiceImpl;
import com.ktjiaoyu.utils.Constants;
import com.ktjiaoyu.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/main/goods")
public class GoodsInfoController extends BaseController{
    private static final Logger LOGGER= LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private GoodsInfoService goodsInfoService;

    @Autowired
    private CategoryService categoryService;

    @RequestMapping(value = "/page",produces = "application/json;charset=UTF-8")
    public String page(Model model, GoodsInfo condition,
                       @RequestParam(value = "pageNo",defaultValue = "1")Integer pageNo,
                       @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize){
        Page<GoodsInfo> page=null;
        try {
            page=goodsInfoService.findPage(condition,pageNo,pageSize);
            List<GoodsCategory> categoryList=categoryService.getAll();

            model.addAttribute("pageInfo",page);
            model.addAttribute("categoryList",categoryList);
            model.addAttribute("condition",condition);
        }catch (Exception e){
            LOGGER.error("GoodsInfoController中的page方法执行发生错误。",e);
            throw new RuntimeException(e);
        }
        return "goods/page";
    }

    @DeleteMapping( "/del/{id}")
    @ResponseBody
    public String del(@PathVariable("id") Long id){
        try {
            int count=goodsInfoService.deleteById(id);
            return ajaxResult();
        }catch (Exception e){
            LOGGER.error("GoodsInfoController中的del方法执行发生错误",e);
            throw new RuntimeException(e);
        }
    }

    @GetMapping(value = "/{id}/view",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map ajaxView(@PathVariable("id") Long id){
        HashMap<String,Object> resultMap=new HashMap<String,Object>();
        GoodsInfo info=null;
        try {
            info=goodsInfoService.findById(id);
            if(info!=null){
                resultMap.put("code",200);
                resultMap.put("data",info);
            }else{
                resultMap.put("code",500);
            }
        }catch (Exception e){
            LOGGER.error("GoodsInfoController中的ajaxView方法执行发生错误",e);
            resultMap.put("code",500);
        }
        return resultMap;
    }
    @GetMapping("/edit")
    public String toEdit(@RequestParam("id") Long id,Model model){
        GoodsInfo goodsInfo=null;
        List<GoodsCategory> categoryList=null;
        try {
            goodsInfo=goodsInfoService.findById(id);
            categoryList=categoryService.getAll();
            model.addAttribute("categoryList",categoryList);
            model.addAttribute("goods",goodsInfo);
        }catch (Exception e){
            LOGGER.error("GoodsInfoController中的toEdit方法执行发生错误",e);
            throw new RuntimeException(e);
        }
        return "goods/save";
    }

    @GetMapping("/save")
    public String toSave(Model model){
        List<GoodsCategory> categoryList=null;
        try {
            categoryList=categoryService.getAll();
            model.addAttribute("categoryList",categoryList);
            model.addAttribute("goods",new GoodsInfo());
        }catch (Exception e){
            LOGGER.error("GoodsInfoController中的toSave方法执行发生错误",e);
            throw new RuntimeException(e);
        }
        return "goods/save";
    }


    @PostMapping("/save")
    public String doSave(GoodsInfo goodsInfo){
        String[] allowImageExt={"jpg","jpeg","png","gif","bmp"};
        try {

            if ((goodsInfo.getId()==null)){
                goodsInfo.setCreateTime(new Date());
            }else {
                goodsInfo.setUpdateTime(new Date());
            }

            boolean ok=goodsInfoService.saveOrUpdate(goodsInfo);
            if (ok){
                goodsInfo.setGoodsCategory(categoryService.findById(goodsInfo.getCategoryId()));
                return redirect("/main/goods/page");
            }
        }catch (Exception e){
            LOGGER.error("GoodsInfoController中的doSave方法执行发生错误",e);
            throw new RuntimeException(e);
        }
        return "goods/save";
    }
}
